/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package adee.qrlogin;

import android.content.Context;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.HashMap;
import qrlogin.common.QRLoginParam;

/**
 *
 * @author adee
 */
public class AccountForm {
    private Account account;
    private HashMap<String,TextView> controls;
    private Context context;
    private LinearLayout layout;


    private void buildForm(){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        TextView link = new TextView(context);
        link.setText(account.getLink());
        link.setLayoutParams(params);
        link.setHint("LoginLink");
        layout.addView(link);
        this.controls.put("link", link);
        buildParameterList();
        
    }

    private void buildParameterList(){

        for(QRLoginParam param:account.getLoginParameters().values()){
            TextView text = this.getControl(param);
            this.controls.put(param.getName(), text);
            this.layout.addView(text);
        }
        
    }

    private TextView getControl(QRLoginParam param){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        TextView result = new EditText(context);
        if (param.getType().equals("PASSWORD")) result.setTransformationMethod(new PasswordTransformationMethod());
        result.setText(param.getValue());
        result.setHint(param.getHumanName());
        result.setLayoutParams(params);
        return result;
    }

    public AccountForm(Account account,Context context) {
        this.controls = new HashMap<String, TextView>();
        this.account = account;
        this.context=context;
        this.layout = new LinearLayout(context);
        this.layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
        layout.setLayoutParams(params);
        this.buildForm();
        
    }

    public LinearLayout getLayout() {
        return layout;
    }

    public void updateAccount(){
        account.setLink(this.controls.get("link").getText().toString());
        for (String key: account.getLoginParameters().keySet()){
            account.getLoginParameters().get(key).setValue(controls.get(key).getText().toString());
        }
    }


}
